

import pytest
from django.utils.timezone import now
from mixer.backend.django import mixer

from cases.models import Case, TeSuite, Steps, CaseResult, SuitReport


@pytest.mark.django_db
def test_case_models():
    mixer.cycle(10).blend('cases.InterfaceInfo')
    mixer.cycle(20).blend('cases.TeSuite')
    mixer.cycle(20).blend('cases.Case')
    mixer.cycle(30).blend('cases.SqlScript')
    mixer.cycle(50).blend('cases.Steps')
    test_suit=TeSuite.objects.all().filter(is_run=1).get(id=8)
    test_cases = test_suit.case.all()
    print(type(test_cases))
    suit_result = SuitReport()
    suit_result.test_suit=test_suit
    suit_result.start_time=now()
    suit_result.save()
    fail_count=0
    pass_count=0
    for cs in test_cases:
        case_result= CaseResult()
        case_result.start_time=now()
        case_result.test_case=cs
        case_result.case_result_suit = suit_result
        steps=cs.steps_set.all()
        log_text=''
        for step in steps:
            print(step.step_description)
            # sleep(3)
            case_result.result_status = 'success'
            try:
                print(123)
            except:
                case_result.result_status = 'fail'
            s= "{}{}".format(str(now()),step.step_description+'\n')
            log_text=log_text.join(s)
        case_result.log_text=log_text
        case_result.end_time = now()
        if case_result.result_status=='success':
            pass_count+=1
        else:
            fail_count+=1
        case_result.save()
    suit_result = SuitReport.objects.get(id=suit_result.id)
    suit_result.end_time=now()
    suit_result.fail_count=fail_count
    suit_result.pass_count=pass_count
    suit_result.save()

    ddd =CaseResult.objects.all()
    for sd in ddd:
        print(sd.end_time,sd.take_time,sd.log_text)
    dfd= SuitReport.objects.all()
    for s in dfd:
        print(s.start_time,s.pass_count,s.fail_count,s.total_count,s.pass_rate)
    print(CaseResult.objects.all().count())